<?php
/**
 * Created by PhpStorm.
 * User: 农夫
 * Date: 2017/9/11
 * Time: 9:50
 */
namespace app;

use app\BaseCollection;
use My\Tool;
use My\CheckAuth;
use app\model\Menu;
use think\Request;

class AdminCollection extends BaseCollection
{

    private $module = null;//模块
    private $action = null;//控制器
    private $controller = null;//方法

    public function __construct()
    {
        parent::__construct();
        $request = Request::instance();
        $this->module = strtolower($request->module());
        $this->controller = strtolower($request->controller());
        $this->action = strtolower($request->action());
        $this->_init();
        $this->checkAccess();
        $this->currentLocation();
    }

    //当前位置
    private function currentLocation()
    {
        $controller_action = $this->controller . '/' . $this->action;
        $findService = Menu::findService(['name' => $controller_action]);
        $data['menu_id'] = $findService['menu_id'];
        $list = Menu::listCurrentService([], $data);
        $this->assign('currentLocation', $list);
    }

    //首先执行相关资料
    private function _init()
    {
        $this->assign('web_title', config('web_title'));
        $this->assign('randnumber', '223344');
        $this->assign('css', config('path.css'));
        $this->assign('images', config('path.images'));
        $this->assign('js', config('path.js'));
        $this->assign('zui', config('path.zui'));
        $this->assign('kindeditor', config('path.kindeditor'));
        $this->assign('listorderUrl', url($this->controller . '/listorder'));
    }

    //验证登录
    public function checkLogin()
    {
        if ($this->getAdmin()) return true;
        return false;
    }

    //登录权限验证
    public function checkAccess()
    {
        $access = array(
            'login', 'publicclass'
        );
        if (in_array($this->controller, $access)) {
            return true; //不判断
        } else {
            //权限验证
            if ($this->checkLogin()) {
                $access_system = array(
                    'index', 'publicclass'
                );
                if (in_array($this->controller, $access_system)) {
                    return true; //不判断
                } else {
                    if ($this->getGroup() != config('default_group_id')) {
                        $controller_action = $this->controller . '/' . $this->action;
                        $checkAuth = new CheckAuth($this->getGroup(), $controller_action);
                        if ($checkAuth->check() === false) {
                            $this->returnError("权限不足");
                        }
                    }
                }
            } else {
                //判断如果没有登录，首先要去登录
                $this->returnSuccess("请先登录", url('login/index'));
            }
        }
    }

    //管理员id
    public function getAdmin()
    {
        return $this->getSession('admin_admin_id');
    }

    //权限组id
    public function getGroup()
    {
        return $this->getSession('admin_group_id');
    }

    //管理员用户名
    public function adminUsername()
    {
        return $this->getSession('admin_username');
    }

}